<template>
  <el-card
    class="dept-energy-pie work-bench-department__card"
    shadow="never"
    body-style="display: flex; flex-direction: column; height: calc(100% - 20px); padding: 10px 15px;"
  >
    <div class="dept-energy-pie__header work-bench-department__card__header">
      <span class="work-bench-department__card__title dept-energy-pie__title">分项用能</span>
    </div>
    <div class="work-bench-department__card__body dept-energy-pie__body">
      <div ref="deptEnergyPieChartRef" class="dept-energy-pie__chart">暂无数据!</div>
    </div>
  </el-card>
</template>

<script>
// ============================== 导入依赖 ============================== //

import dashboardDeptApi from '@admin/views/Workbench/WorkbenchDepartment/api'
import { drewPie } from './pie'

// ============================== 导出组件 ============================== //

export default {
  /**
   * 名称定义 (实际使用名称)
   */
  name: 'DeptEnergyPie',

  props: {
    /**
     * 部门ID
     */
    organId: { type: String, default: '' },
  },

  methods: {
    /**
     * @function 分项用能
     * @param {String} organId -部门ID
     */
    queryDeptEnergyPie(organId) {
      if (!organId) return

      dashboardDeptApi.queryDeptEnergyPie({ type: 2, organId }).then((res) => {
        const first = res.info[0] || {}
        drewPie(this.$refs.deptEnergyPieChartRef, (first.info || []).map((item) => {
          return {
            name: item.label,
            value: item.value,
            unit: item.unit,
          }
        }))
      })
    },
  },

  mounted() {
    this.queryDeptEnergyPie(this.organId)
  },

  watch: {
    organId(newVal) {
      this.queryDeptEnergyPie(newVal)
    },
  },
}
</script>

<style lang="scss" scoped>
@import './index.scss';
</style>
